package com.exsun.gateway.jt809.common;

import com.exsun.gateway.jt809.data.PushData;
import io.netty.channel.Channel;

import java.util.ArrayList;
import java.util.List;

public class LinkMgr {
    // 链路通道标识
    private static Channel MASTER_CHANNEL = null;
    private static Channel SLAVE_CHANNEL = null;
    //
    private static boolean masterChannelLoggingStatus = false;
    private static boolean slaveChannelLoggingStatus = false;

    public static void setMasterChannel(Channel masterChannel) {
        if(masterChannel == null && MASTER_CHANNEL != null){
            FlowIdMgr.removeChannelId(MASTER_CHANNEL.id());
        }
        MASTER_CHANNEL = masterChannel;
    }

    public static void setSlaveChannel(Channel slaveChannel) {
        if(slaveChannel == null&& SLAVE_CHANNEL != null){
            FlowIdMgr.removeChannelId(SLAVE_CHANNEL.id());
        }
        SLAVE_CHANNEL = slaveChannel;
    }
    /**
     * 优先link发送消息，如果link不通，应通过备用链路发送
     *
     * @param msgId
     * @param msgBodyBytes
     * @param link         0主链路  1从链路
     * @return
     */
    public static boolean push2FitChannel(int msgId, byte[] msgBodyBytes, LinkType link) {
        PushData push = new PushData(msgId, msgBodyBytes);
        List<PushData> pushs = new ArrayList<>();
        pushs.add(push);
        return push2FitChannel(pushs, link);
    }
    /**
     * 优先link发送消息，如果link不通，应通过备用链路发送
     * @param push
     * @param link 0主链路  1从链路
     * @return
     */
    public static boolean push2FitChannel(List<PushData> push, LinkType link){
        Channel channel = getFitLink(link);
        if (channel == null||!channel.isActive()) {
            return false;
        }
        if(push == null||push.size() == 0)return true;
        channel.writeAndFlush(push);
        return true;
    }

    public static boolean getLinkStatus(){
        if(masterChannelLoggingStatus||slaveChannelLoggingStatus)return true;
        return false;
    }

    public static boolean push2FixedChannel(List<PushData> push, Channel channel){
        if(push == null||push.size() == 0)return true;
        if (channel == null||!channel.isActive()) {
            return false;
        }
        channel.writeAndFlush(push);
        return true;
    }


    public static boolean push2FixedChannel(List<PushData> push, LinkType link){
        if(push == null||push.size() == 0)return true;
        Channel channel = (link == LinkType.MASTER ? MASTER_CHANNEL:SLAVE_CHANNEL);
        if (channel == null||!channel.isActive()) {
            return false;
        }
        channel.writeAndFlush(push);
        return true;
    }


    // 通过指定的链路提交消息
    public static boolean push2FixedChannel(int msgId, byte[] msgBodyBytes, Channel channel) {
        if (channel == null||!channel.isActive())
            return false;
        PushData push = new PushData(msgId,msgBodyBytes);
        List<PushData> pushs = new ArrayList<>();
        pushs.add(push);
        channel.writeAndFlush(pushs);
        return true;
    }

    public static boolean push2FixedChannel(int msgId, byte[] msgBodyBytes, LinkType link){
        Channel channel = (link == LinkType.MASTER ? MASTER_CHANNEL:SLAVE_CHANNEL);
        if (channel == null||!channel.isActive()) {
            return false;
        }
        PushData push = new PushData(msgId, msgBodyBytes);
        List<PushData> pushs = new ArrayList<>();
        pushs.add(push);
        channel.writeAndFlush(pushs);
        return true;
    }

    /**
     * 获取优先link，如果link不通，应取备用链路
     * @param link 0主链路  1从链路
     * @return
     */
    private static Channel getFitLink(LinkType link) {
        Channel channel = null;
        if (link == LinkType.MASTER) {
            if ((MASTER_CHANNEL != null&&masterChannelLoggingStatus)) {
                channel = MASTER_CHANNEL;
            } else if ((SLAVE_CHANNEL != null&&slaveChannelLoggingStatus)) {
                channel = SLAVE_CHANNEL;
            }
        } else if (link == LinkType.SLAVE) {
            if ((SLAVE_CHANNEL != null&&slaveChannelLoggingStatus)) {
                channel = SLAVE_CHANNEL;
            } else if ((MASTER_CHANNEL != null&&masterChannelLoggingStatus)) {
                channel = MASTER_CHANNEL;
            }
        }
        return channel;
    }

    public static boolean isMasterChannelLoggingStatus() {
        return masterChannelLoggingStatus;
    }

    public static void setMasterChannelLoggingStatus(boolean masterChannelLoggingStatus) {
        LinkMgr.masterChannelLoggingStatus = masterChannelLoggingStatus;
    }

    public static boolean isSlaveChannelLoggingStatus() {
        return slaveChannelLoggingStatus;
    }

    public static void setSlaveChannelLoggingStatus(boolean slaveChannelLoggingStatus) {
        LinkMgr.slaveChannelLoggingStatus = slaveChannelLoggingStatus;
    }
}
